package com.netflexitysolutions.amazonws.s3.internal.operations;

import org.apache.commons.codec.binary.Base64;

import com.amazonaws.s3.doc._2006_03_01.PutObjectInline;
import com.amazonaws.s3.doc._2006_03_01.PutObjectResult;
import com.netflexitysolutions.amazonws.s3.S3;
import com.netflexitysolutions.amazonws.s3.S3Object;
import com.netflexitysolutions.amazonws.s3.S3ServerResponse;
import com.netflexitysolutions.amazonws.s3.internal.utils.S3Utils;

public class PutObjectInlineOperation extends OperationExecutor<PutObjectInline, S3ServerResponse>{

    private S3Object object;
    private String bucket;

    public PutObjectInlineOperation(S3 s3, S3Object object, String bucket){
        super(s3);
        this.bucket = bucket;
        this.object = object;
    }

    @Override
    protected S3ServerResponse call(PutObjectInline request) {
        request.setKey(object.getKey());
        request.setAccessControlList(S3Utils.convert(object.getAcl()));
        request.setContentLength(object.getContentLength());

        request.setData(Base64.encodeBase64(object.getData()));
        
        if(object.getMetadata() != null){
            S3Utils.fillMetadata(request.getMetadata(), object.getMetadata());
        }
        request.setBucket(bucket);
        injectCredentials(request, "PutObjectInline", getS3().getAccessKey(), getS3().getSecretKey());

        PutObjectResult result = getS3().getService().putObjectInline(request).getPutObjectInlineResponse();
        return new S3ServerResponse(result.getETag(), result.getLastModified());
    }
}